package com.dragon.design.pattern.creational.singleton;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/5/15 21:29
 */
public class HungrySingletonTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
//        HungarySingleton instance = HungarySingleton.getInstance();
//        System.out.println("instance:" + instance);
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("hungrySingleton_file")));
//        oos.writeObject(instance);
//        oos.flush();
//        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("hungrySingleton_file")));
//        HungarySingleton newInstance = (HungarySingleton) ois.readObject();
//        System.out.println("newInstance:" + newInstance);
//        System.out.println(instance == newInstance);
//        ois.close();
//        oos.close();
        HungarySingleton instance = HungarySingleton.getInstance();
        System.out.println("instance:" + instance);
        Class c = HungarySingleton.class;
        Constructor constructor = c.getDeclaredConstructor();
        constructor.setAccessible(true);
        HungarySingleton newInstance = (HungarySingleton) constructor.newInstance();
        System.out.println("newInstance:" + newInstance);
        System.out.println(instance == newInstance);
    }
}
